package LitText;
import java.io.*;
//import java.util.*;

public class IO {

    static String Path;
    
    
    static void Test(){
        GetText("uni.txt");
        p("=================================");
    }
    
    static void p(String str) {
        System.out.println("IO:"+str);
    }
    
    static char ByteToChar(byte b1, byte b2){
        
        char c=0;
            c |= b2;
            c <<=8;
            c |= b1;
            System.out.println("ByteToChar");
            return c;
    }
    
    static byte[] CharToByte(char c){
        
        byte b1;
        byte b2;
        
        b2 = (byte)(c>>>8);
        b1 = (byte)(c^((byte)b2<<16));
        System.out.println("CharToByte");
        return new byte[]{b1,b2};
        
    }

	public static void GetText(String str){
	byte[] bytesToWrite = {-46,-27,-15,-14,-18,-30,-32,-1,13,10,-15,-14,-16,-18,-22,-32,(byte)('я')};
	
	String fileName = "C:/"+str;
	try {
/*		// создать выходной поток
	FileOutputStream outFile = new FileOutputStream(fileName);
	System.out.println("File is open to write");
	// записать массив
	outFile.write(bytesToWrite);
	System.out.println("Written: " + bytesToWrite.length + " byte");
	// по окончании использования должен быть закрыт
	outFile.close();
	System.out.println("Output stream is closed");
	// создать входной поток
*/	
        FileInputStream inFile = new FileInputStream(fileName);
	System.out.println("File is open to read");
	// узнать, сколько байт готово к считыванию
	int bytesAvailable = inFile.available();
        byte[] bytesReaded = new byte[bytesAvailable];
	System.out.println("Ready to read: " + bytesAvailable + " byte");
	// считать в массив
	int count = inFile.read(bytesReaded,0,bytesAvailable);
	System.out.println("Read: " + count + " byte");
	String txt = "";
      
        if (ByteToChar(bytesReaded[1],bytesReaded[0]) == 65534 ) {
        for (int i=2;i<count;i++){
            txt += ByteToChar(bytesReaded[i++],bytesReaded[i]);
        }
        System.out.println();
        System.out.println(txt);
	
        System.out.println();
        }
        else System.out.println("Текст должен быть в кодировке Unicode");
	byte a=3,b=4;
        byte[] x;
        x=CharToByte(ByteToChar(a,b));
        System.out.println(x[0]+", "+x[1]);
        
        inFile.close();
	System.out.println("Input stream is closed");
	} catch (FileNotFoundException e) {
	System.out.println("Its impossible to write to file: " + fileName);
	} catch (IOException e) {
	System.out.println("Input/output error: " + e.toString());
        
	}
	}
            
    }
